package com.liuh.ssh2.dao;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import com.liuh.ssh2.pojo.User;

@Repository("userDao")
public class UserDao2 extends HibernateDaoSupport{


	/**
	 * @note 根据id 获取user
	 * @author liuh
	 * */
	public User getUserById(int id) {
		return (User)getHibernateTemplate().get(User.class, id);
	}
	
	/**
	 * @note 根据id 获取user
	 * @author liuh
	 * */
	public User getUserById(User u) {
		return (User)getHibernateTemplate().findByValueBean("from User where id=:id", u).get(0);
	}
	
	/**
	 * @note 新增user
	 * @author liuh
	 * */
	public void saveUser(User u) {
		getHibernateTemplate().save(u);
	}
	
	/**
	 * @note 新增user
	 * @author liuh
	 * */
	public void updUser(User u) {
		getHibernateTemplate().saveOrUpdate(u);
	}
	
	/**
	 * @note hibernate 分页
	 * @author liuh
	 * */
	public List<User> getUserPage(int pageNo,int pageSize) {
		return getHibernateTemplate().execute(new HibernateCallback<List<User>>() {
			@Override
			public List<User> doInHibernate(Session session) throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Query query=session.createQuery("from User");
				query.setFirstResult(pageNo);
				query.setMaxResults(pageSize);
				return query.list();
			}
		});
	}
}
